package com.shaoshaohuo.app.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.shaoshaohuo.app.R;

/**
 * Created by Administrator on 2017/6/20.
 */

public class MyStarView extends View {

    private Paint paint;

    public MyStarView(Context context) {
        this(context, null);
    }

    public MyStarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyStarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);


        xbimap(bitmap);
        canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, getWidth(), getHeight()), null);
    }

    private void xbimap(Bitmap bitmap) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.parseColor("#ff7916"));
        Canvas canvas = new Canvas(bitmap);
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.star);
        canvas.drawBitmap(bitmap1, new Rect(0, 0, bitmap1.getWidth(), bitmap1.getHeight()), new Rect(0, 0, getWidth(), getHeight()), paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawRect(0, 0, rw, getHeight(), paint);
    }

    private float rw;
    public void setRw(float rw) {
        this.rw = rw;
        invalidate();
    }

}
